package com.tanhua.recomment.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.mongo.VideoScore;
import com.itheima.dubbo.api.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 阿鹏
 * @date 2021/2/26 17:09
 */
@Component
@RocketMQMessageListener(topic = "tanhua-video", consumerGroup = "tanhua-video-group")
public class VideoMQListener implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Reference
    private VideoApi videoApi;

    @Override
    public void onMessage(String message) {
        Map map = JSON.parseObject(message, Map.class);

        Long userId = Long.valueOf(map.get("userId").toString());
        Long videoId = Long.valueOf(map.get("videoId").toString());
        Integer type = (Integer) map.get("type");

        VideoScore score = new VideoScore();
        score.setUserId(userId);
        score.setVideoId(videoId);
        score.setDate(System.currentTimeMillis());

        switch (type) {
            case 1:
                score.setScore(20d);
                break;
            case 2:
                score.setScore(5d);
                break;
            case 3:
                score.setScore(-5d);
                break;
            case 4:
                score.setScore(10d);
                break;
            default:
        }

        mongoTemplate.save(score);
    }
}
